package service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import service.PlanService;
import dao.MplanMapper;
import dao.PlanMapper;
import entity.Mplan;
import entity.Plan;
@Service
public class PlanServiceImpl implements PlanService{
	@Autowired
	private PlanMapper planMapper;
	@Autowired
	private MplanMapper mm;
	
	public List<Plan> getAll(Plan plan){
		plan.setName(plan.getName().trim());
		plan.setMaker(plan.getMaker().trim());
		plan.setContent(plan.getContent().trim());
		return planMapper.getAll(plan);
	}
	public boolean addPlan(Plan plan){
		try {
           
			planMapper.addPlan(plan);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public boolean delete(int id){
		try {
			planMapper.delete(id);
			mm.delete(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public Plan one(int id){
		
		return planMapper.one(id);
	}
	public boolean update(Plan plan){
		try {
			planMapper.update(plan);
			Mplan mp=new Mplan();
			mp.setPid(plan.getId());
			mp.setPname(plan.getName());
			mm.uppname(mp);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return true;
		
	}
	public int pages(int size){
		int pages=0;
		try {
			int count=planMapper.count();
			pages=(count%size==0)?(count/size):(count/size+1);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return pages;
		
	}
	
	public List<Plan> getPage(Plan plan){
		List<Plan> list=null;
		try {
			list=planMapper.getPage(plan);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}
	

}
